﻿/*  ----------------------------------------------------------------------------
 *  EuphoricNation
 *  ----------------------------------------------------------------------------
 *  EuphoricEngine v1.0
 *  ----------------------------------------------------------------------------
 *  File:       Engine.cs
 *  Author:     Shane-PC\Shane
 *  
 *  Credits:    Sean James's Game Engine Tutorial, Got Me Started (See Below)
 *  
 *  Tutorial:   'http://www.innovativegames.net/blog/2009/10/21/game-engine-tutorial-part-iii-chapter-4-engine/'
 *  
 *  ----------------------------------------------------------------------------
 */

using System;
using System.Collections.Generic;
using System.ComponentModel.Design;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

using FarseerGames.FarseerPhysics;

namespace EuphoricEngine
{
    public class Engine
    {
        private Game _game = null;
        private GameTime _gameTime = null;

        private IEContentManager _content = null;
        private GraphicsDevice _graphicsDevice = null;
        private SpriteBatch _spriteBatch = null;
        private IServiceContainer _services = null;

        private List<GameScreen> _gameScreens = new List<GameScreen>();

        public event EventHandler OnShutdown;

        public Engine(Game game, GraphicsDeviceManager graphics)
        {
            _game = game;

            _services = new ServiceContainer();
            _services.AddService(typeof(IGraphicsDeviceService), graphics);
            _services.AddService(typeof(IGraphicsDeviceManager), graphics);

            _graphicsDevice = graphics.GraphicsDevice;
            _content = new IEContentManager(Services, "Content");
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            _graphicsDevice = GraphicsDevice;
        }

        public void PushGameScreen(GameScreen gameScreen)
        {
            if (gameScreen.Engine != null)
                throw new Exception("This GameScreen already exists on the stack of another Engine instance!");

            if (!_gameScreens.Contains(gameScreen))
            {
                _gameScreens.Add(gameScreen);
                gameScreen.Engine = this;
                gameScreen.LoadGameScreen();
            }
        }

        public GameScreen PopGameScreen()
        {
            if (_gameScreens.Count == 0)
                return null;

            GameScreen gameScreen =  _gameScreens[_gameScreens.Count - 1];

            gameScreen.Engine = null;
            _gameScreens.Remove(gameScreen);

            return gameScreen;
        }

        public void Update(GameTime gameTime)
        {
            _gameTime = gameTime;

            List<GameScreen> updating = new List<GameScreen>();

            foreach (GameScreen gameScreen in _gameScreens)
                updating.Add(gameScreen);

            foreach (GameScreen gameScreen in updating)
                gameScreen.Update();
        }

        public void Draw(GameTime gameTime)
        {
            _gameTime = gameTime;

            foreach (GameScreen gameScreen in _gameScreens)
                gameScreen.Draw();
        }

        public void ShutDown()
        {
            _game.Exit();
        }

        public Game Game { get { return _game; } }
        public GameTime GameTime { get { return _gameTime; } }
        public IEContentManager Content { get { return _content; } }
        public GraphicsDevice GraphicsDevice { get { return _graphicsDevice; } }
        public SpriteBatch SpriteBatch { get { return _spriteBatch; } }
        public IServiceContainer Services { get { return _services; } }
    }
}
